package com.dgut.demo.service.impl;

import com.dgut.demo.bean.EmployeesMessage;
import com.dgut.demo.bean.PurchaseMessage;
import com.dgut.demo.bean.SupplierMessage;
import com.dgut.demo.bean.User;
import com.dgut.demo.controller.EmployeesController;
import com.dgut.demo.controller.LogsController;
import com.dgut.demo.controller.SupplierController;
import com.dgut.demo.mapper.PurchaseMapper;
import com.dgut.demo.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/2/23 21:14
 * @Version 1.0
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private SupplierController supplierController;
    @Autowired
    private LogsController logsController;
    @Autowired
    private User user;

    @Override
    public List<PurchaseMessage> getAllPurchaseMessage() {
        return purchaseMapper.getAllPurchaseMessage();
    }

    @Override
    public List<PurchaseMessage> getPurchaseMessageByPid(String pid) {
        return purchaseMapper.getPurchaseMessageByPid(pid);
    }

    @Override
    public List<PurchaseMessage> getPurchaseMessageByPsid(String psid) {
        return purchaseMapper.getPurchaseMessageByPsid(psid);
    }

    @Override
    public List<PurchaseMessage> getPurchaseMessageByEname(String ename) {
        return purchaseMapper.getPurchaseMessageByEname(ename);
    }

    @Override
    public List<PurchaseMessage> getPurchaseMessageByMaterial(String material) {
        return purchaseMapper.getPurchaseMessageByMaterial(material);
    }

    @Override
    public int addOnePurchaseMessage(PurchaseMessage purchaseMessage) {
        SupplierMessage supplierMessage = supplierController.getSupplierMessageById(purchaseMessage.getPsid()).get(0);

        double pmoney = supplierMessage.getSprice()*purchaseMessage.getPcounts();
        purchaseMessage.setPmoney(pmoney);
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyMMddHHmmss");
        int random = (int)(Math.random()*10);
        String pid='5'+ dateFormat1.format(date) + random;
        purchaseMessage.setPid(pid);
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ptime = dateFormat2.format(date);
        purchaseMessage.setPtime(ptime);
        int flag =  purchaseMapper.addOnePurchaseMessage(purchaseMessage);
        if(flag == 1){

            //log
            String actions = "新增进货单【编号:"+pid+",材料:"+supplierMessage.getSname()+",数量:"+purchaseMessage.getPcounts()+",总额:"+pmoney+",负责人:"+supplierMessage.getSname()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
            return 1;
        }else{
            return 0;
        }
    }
}
